//
//  Date+Extension.swift
//  LiveTrivia
//
//  Created by Gesen on 2017/11/30.
//  Copyright © 2017年 LiveTrivia. All rights reserved.
//

import Foundation

extension Date {
    
    var isToday: Bool {
        return Calendar.current.isDateInToday(self)
    }
    
    var day: String {
        var current = Calendar.current
        current.locale = Locale(identifier: "zh-Hans_CN")
        if current.isDateInToday(self) {
            return "今天"
        } else if current.isDateInTomorrow(self) {
            return "明天"
        } else if current.isDateInYesterday(self) {
            return "昨天"
        } else {
            if let week = current.dateComponents([.weekday], from: self).weekday {
                return current.shortWeekdaySymbols[week - 1]
            }
            return format("d日")
        }
    }
    
    var ampm: String {
        let formatter = DateFormatter()
        formatter.amSymbol = "上午"
        formatter.pmSymbol = "下午"
        formatter.dateFormat = "aaa"
        return formatter.string(from: self)
    }
    
    func timeSinceNowString() -> String{
        let interval = fabs(self.timeIntervalSinceNow)
        let aDayInterval = TimeInterval(24*3600)
        if interval < aDayInterval {
            return format("HH:mm")
        } else if interval < 2 * aDayInterval {
            return "昨天 " + format("HH:mm")
        } else {
            return format("yyyy-MM-dd HH:mm")
        }
    }
    
    func timePassed() -> String {
        let date = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents(
            [.year, .month, .day, .hour, .minute, .second],
            from: self, to: date)
        
        if components.year ?? 0 >= 1 {
            return "\(components.year!)年前"
        } else if components.month! >= 1 {
            return "\(components.month!)月前"
        } else if components.day! >= 1 {
            return "\(components.day!)天前"
        } else if components.hour! >= 1 {
            return "\(components.hour!)小时前"
        } else if components.minute! >= 1 {
            return "\(components.minute!)分钟前"
        } else if components.second! >= 1 {
            return "\(components.second!)秒前"
        } else {
            return "刚刚"
        }
    }
    
    func format(_ f: String = "yyyy-MM-dd HH:mm:ss", locale: Locale = .current) -> String {
        let formatter = DateFormatter()
        formatter.locale = locale
        formatter.dateFormat = f
        return formatter.string(from: self)
    }
}

extension Date {
    
    var constellation: String? {
        guard let date = string(withFormat: "MM.dd").double() else {
            return nil
        }
        let double = date.rounded(numberOfDecimalPlaces: 2, rule: .up)
        switch double {
        case 1.20...2.18 :      return "水瓶座"
        case 2.19...3.20 :      return "双鱼座"
        case 3.21...4.19 :      return "白羊座"
        case 4.20...5.20 :      return "金牛座"
        case 5.21...6.21 :      return "双子座"
        case 6.22...7.22 :      return "巨蟹座"
        case 7.23...8.22 :      return "狮子座"
        case 8.23...9.22 :      return "处女座"
        case 9.23...10.23 :     return "天秤座"
        case 10.24...11.22 :    return "天蝎座"
        case 11.23...12.21 :    return "射手座"
        case 12.22...12.31 :    return "摩羯座"
        case 1.01...1.19 :      return "摩羯座"
        default:                return nil
        }
    }
}
